package com.sanjiahantang.ythshopping.adapter.home;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.sanjiahantang.ythshopping.R;
import com.sanjiahantang.ythshopping.activity.MainActivity;
import com.sanjiahantang.ythshopping.model.HomePageData;

import java.util.List;

/**
 * Created by Administrator on 2017/6/8.
 */

public class HomeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final Context context;
    //布局标识集合
    private final List<Integer> typeList;
    private LayoutInflater inflater;

    private HomePageData data;
    private MainActivity activity;

    //各种类型
    private final int TYPE_BANNER = 1;//轮播图
    private final int TYPE_CLASSIFY = 2;//专题列表
    private final int TYPE_IMAGE = 4;//图片
    private final int TYPE_IMAGE2 = 6;//图片
    private final int TYPE_ALLIANCE=3;//联盟商家
    private final int TYPE_NEWPRO = 5;//新品上市
    private final int TYPE_HOTSHOP = 7;//热销商品
    private int colorBG = 0;

    public HomeAdapter(Context context, List<Integer> typeList) {
        this.context = context;
        this.typeList = typeList;
        this.inflater = LayoutInflater.from(context);
        colorBG = this.context.getResources().getColor(R.color.bg_color);
    }

    public HomeAdapter(Context context, List<Integer> typeList, HomePageData data, MainActivity activity) {
        this(context, typeList);
        this.data = data;
        this.activity = activity;
    }

    @Override
    public int getItemViewType(int position) {

        return typeList.get(position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        switch (viewType) {
            case TYPE_BANNER:   //轮播图和输入框
                return new BannerHolder(inflater.inflate(R.layout.rv_item_banner, null),
                        context, data.getAds());
            case TYPE_CLASSIFY: //功能菜单
                return new ClassifyHolder(inflater.inflate(R.layout.rv_item_classify, null),
                        context, data.getMenu(), activity,GvSubjectAdapter.TYPE_HOME);
            case TYPE_IMAGE:    //图片

                return new ImageHolder(context, inflater.inflate(R.layout.item_image, null),data.getAd1() == null ? "" : data.getAd1().getAd1_image(),data.getAd1() == null ? "" : data.getAd1().getAd1_link());
            case TYPE_IMAGE2:    //图片

                return new ImageHolder(context, inflater.inflate(R.layout.item_image, null),data.getAd1() == null ? "" : data.getAd2().getAd1_image(),data.getAd2() == null ? "" : data.getAd2().getAd1_link());

            case TYPE_NEWPRO:   //新品上市

                return new ComprehensiveHolder(inflater.inflate(R.layout.rv_item_comprehesiva, null),
                        TYPE_NEWPRO,
                        context, data.getNews(),"","");
            case TYPE_HOTSHOP:  //热销商品
                return new ComprehensiveHolder(inflater.inflate(R.layout.rv_item_comprehesiva, null),
                        TYPE_HOTSHOP,
                        context, data.getHot(),"","");
            case TYPE_ALLIANCE:
                return new AllianceHolder(context,inflater.inflate(R.layout.rv_item_classify, null),data.getShop(),AllianceHolder.TYPE_HOME);

        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyBaseViewHolder) {
            ((MyBaseViewHolder) holder).bindData();
        }
    }

    @Override
    public int getItemCount() {
        return typeList.size();
    }


}
